﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Shapes;
using WCintiqSettings.Dialogs;
using System.Windows.Interop;
using System.Security.Permissions;
namespace WCintiqSettings
{
	/// <summary>
	/// Interaction logic for PenDigitizer3D.xaml
	/// </summary>
	public partial class PenDigitizer3D : Window
	{
		//PT_ or DT_ ?
		public const int CINTIQ = 0x10000;
		public const int INTUOS = 0x20000;
		
		//K, Z, or H?
		public const int ZPEN = 0x02000;
		public const int KPEN = 0x04000;
		public const int HFLG = 0x01000;  //This isn't really it's own pen, just touch added
		
		//Cintiq Sizes
		public const int C12 = 0x120;
		const int C13 = 0x130;
		const int C20 = 0x200;
		const int C21 = 0x210;
		const int C22 = 0x220;
		const int C24 = 0x240;

		//Multiple models with that size?  DTK-2242/2241/2200


		//Z Pen Cintiqs;
		const int DTZ1200 = 0x12;
		const int DTZ2000 = 0x20;
		const int DTZ2100 = 0x21;

		//K Pen Cintiqs 
		public const int DTK1300 = 0x121;
		public const int DTK2100 = 0x122;
		public const int DTK2200 = 0x124;
		public const int DTK2400 = 0x128;
		
		//H Cintiqs


		//ZPen Intuos


		//KPen Intuos
		const int PTK440 = 0x0;

		//Touch Intuos


		StylusPointDescription penDescription;

		WCintiqHID.Cintiq_K wc21ux;

		public PenDigitizer3D(WCintiqHID.Cintiq_K inWacom21UX, int WhichTablet)
		{
			this.InitializeComponent();
			wc21ux = inWacom21UX;

			penDescription = new StylusPointDescription(new StylusPointPropertyInfo[] {

					    new StylusPointPropertyInfo(StylusPointProperties.X),
					    new StylusPointPropertyInfo(StylusPointProperties.Y),
					    new StylusPointPropertyInfo(StylusPointProperties.NormalPressure),
					    new StylusPointPropertyInfo(StylusPointProperties.Z),
					    new StylusPointPropertyInfo(StylusPointProperties.XTiltOrientation),
					    new StylusPointPropertyInfo(StylusPointProperties.YTiltOrientation),
					    new StylusPointPropertyInfo(StylusPointProperties.BarrelButton)

			});

			switch (WhichTablet)
			{
				case DTK2100:
					break;
				case DTK2400:
					break;
				case DTZ1200:
					break;
				default:
					break;
			}

		}

		private void button_Click(object sender, RoutedEventArgs e)
		{
			this.Close();
		}

		private void Window_StylusInAirMove(object sender, StylusEventArgs e)
		{
			Transform3DGroup myT3DGroup = new Transform3DGroup();

			double x = e.GetPosition(this).X;
			double y = e.GetPosition(this).Y;
			double z;
			int xt, yt;

			StylusPointDescription wndSPDesc= e.GetStylusPoints(this).Description;
			StylusPointDescription commonDisc = StylusPointDescription.GetCommonDescription(wndSPDesc, penDescription);

			StylusDevice myStylus = e.StylusDevice;
			StylusPointCollection points = myStylus.GetStylusPoints(this, commonDisc);

			
			x = ((x/1600) * 41.02);
			x += (2.8275);

			
			y = (9.071*2)-((y/1200) * (9.071*2));
			
			y += 14.987025;

			xt = points[0].GetPropertyValue(StylusPointProperties.XTiltOrientation);
			yt = points[0].GetPropertyValue(StylusPointProperties.YTiltOrientation);

			textBox.Text = String.Format("Current Position: {0:N1}, {1:N1}, {2:N}, {3:N}", new object[] { x, y, xt, yt});

	

			//StylusDevice currentStylus = Stylus.CurrentStylusDevice;
			//StylusPointDescription description1 =
			//	 new StylusPointDescription(new StylusPointPropertyInfo[]
			//		{
			//		    new StylusPointPropertyInfo(StylusPointProperties.X),
			//		    new StylusPointPropertyInfo(StylusPointProperties.Y),
			//		    new StylusPointPropertyInfo(StylusPointProperties.NormalPressure),
			//		    new StylusPointPropertyInfo(StylusPointProperties.XTiltOrientation),
			//		    new StylusPointPropertyInfo(StylusPointProperties.YTiltOrientation),
			//		    new StylusPointPropertyInfo(StylusPointProperties.BarrelButton)
			//		});

			//StylusPointDescription description2 = currentStylus.GetStylusPoints(inkCanvas1).Description;

			//StylusPointDescription description3 =
			//    StylusPointDescription.GetCommonDescription(description1, description2);

			//StylusPointCollection points = currentStylus.GetStylusPoints(inkCanvas1, description3);

			myT3DGroup.Children.Add(new System.Windows.Media.Media3D.TranslateTransform3D(x, 1 , -y));
			myT3DGroup.Children.Add(new System.Windows.Media.Media3D.ScaleTransform3D(0.5, 0.5, 0.5));
			myT3DGroup.Children.Add(new System.Windows.Media.Media3D.RotateTransform3D(
					new System.Windows.Media.Media3D.AxisAngleRotation3D(
						new System.Windows.Media.Media3D.Vector3D(1, 0,0),
						180.0)
						));

			DTK2400_3D.GripPen.Transform = myT3DGroup;

			//AxisAngleRotation3D Axis="0.024,0.258,0.966" Angle="194.455"
		}

		private void Window_StylusInRange(object sender, StylusEventArgs e)
		{

		}

		private void Window_StylusOutOfRange(object sender, StylusEventArgs e)
		{

		}


		// 1600 - 2.827500 <-> 23.337502 = 20.51 = 0.1281875 pts 
		// 1200 - -0.987025 <-> 7.010440 = 7.997465 = 0.0066645542 pts




	}
}